<template>
	<!-- 库内移位新增  -->
	<view class="">
		
		<view class="form-container bg-white">
			<!-- 主表 -->
			<view class="radius  light">

				<view class="flex flex-wrap wrap-row">
					<view class="basis-xs">
						集团<span class="text-red">*</span>
					</view>
					<view class="basis-xl text-black position-r" :class="activeLineIndex == 5 ? 'solid-bottom-blue' :'' "
					 @tap="activeIndexM(1,'GROUP')">
						<input class="wc-input " name="STORENAME" type="text" disabled :value="mainOrder.GROUPNAME" placeholder="请选择" />
						<view class="basis-xs text-right position-a down-arrow-btn ">
							<text class="cuIcon-triangledownfill text-xxl text-bold" :class="activeLineIndex == 2 ? 'text-blue' :'' "></text>
						</view>
					</view>
				</view>
				
				<view class="flex flex-wrap wrap-row">
					<view class="basis-xs">
						移出组织<span class="text-red">*</span>
					</view>
					<view class="basis-xl text-black position-r" :class="activeLineIndex == 5 ? 'solid-bottom-blue' :'' "
					 @tap="activeIndexM(2,'BA_ORGANIZE')">
						<input class="wc-input " name="STORENAME" type="text" disabled :value="mainOrder.ORGNAME" placeholder="请选择" />
						<view class="basis-xs text-right position-a down-arrow-btn ">
							<text class="cuIcon-triangledownfill text-xxl text-bold" :class="activeLineIndex == 2 ? 'text-blue' :'' "></text>
						</view>
					</view>
				</view>

				<view class="flex flex-wrap wrap-row">
					<view class="basis-xs">
						仓库<span class="text-red">*</span>
					</view>
					<view class="basis-xl text-black position-r" :class="isActiveStore? 'solid-bottom-blue' :'' ">
						<input class="wc-input" disabled style="width: 60%;" name="STORENAME" type="text" :value="mainOrder.STORENAME" placeholder="请选择" />
						<view class="basis-xs text-right position-a scan-btn " @tap="openScan('STORENAME')">
							<text class="cuIcon-scan text-xxl text-bold"></text>
						</view>
					</view>
				</view>
				<view class="flex flex-wrap wrap-row">
					<view class="basis-xs">
						移位人<span class="text-red">*</span>
					</view>
					<view class="basis-xl text-black">
						{{ mainOrder.MOVER || '' }}
					</view>
				</view>

				<view class="flex flex-wrap wrap-row">
					<view class="basis-xs">
						物资条码<span class="text-red">*</span>
					</view>
					<view class="basis-xl text-black position-r" :class="isActiveStore? 'solid-bottom-blue' :'' ">
						<input class="wc-input" style="width: 60%;" name="STORENAME" type="text" :value="mainOrder.STORENAME" />
						<view class="basis-xs text-right position-a scan-btn" @tap="openScan('PK_MATERIAL')">
							<text class="cuIcon-scan text-xxl text-bold"></text>
						</view>
					</view>
				</view>
				
				<view class="flex flex-wrap wrap-row">
					<view class="basis-xs">
						备注:
					</view>
					<view class="basis-xl text-black" :class="isActiveNote? 'solid-bottom-blue' :'' ">
						<input @focus="activeNote" @blur="getNote" class="wc-input " type="text" value="" placeholder="请输入" />
					</view>
				</view>
				
			</view>
		</view>
		<view class="form-container">
			<button class="cu-btn block bg-blue margin-tb-sm lg" @click="submit"
			:loading="isDisabled"
			:disabled="isDisabled"
			>
				提交
			</button>
		</view>

		<w-picker mode="dateTime" startYear="2000" endYear="2050" step="1" :defaultVal="[1,1,1,1,2,5]" :current="true"
		 @confirm="onConfirm" ref="dateTime" themeColor="#f00"></w-picker>

	</view>

</template>
<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import wPicker from "@/components/w-picker/w-picker.vue";
	import api from '@/common/api.js';
	import utils from '@/common/util.js';
	// var graceChecker = require("../../common/graceChecker.js");
	let query = {
		"category": "ST_INAPPLY",
		"page": 1,
		"rows": "50",
		"sidx": "",
		"sord": "",
		"queryJson": {},
		"userId": "",
		"account": "admin"
	}
	let _self, dateTimekey, activeIndex, VMEMO, BILLFROM, ajaxUrl, activeType;
	var sourceType = [
		['camera'], //拍照
		['album'], //相册
		['camera', 'album'] //拍照或相册
	]
	var sizeType = [
		['compressed'], //压缩
		['original'], //原图
		['compressed', 'original'] //压缩或原图
	]
	export default {
		computed: mapState(['userInfo', 'baseUrl']),
		data() {
			return {
				bstype: '',
				mainOrder: {},
				isDisabled:false,
				list: [], //获取到的物资数据
				dataList: [],
				allData: [], //初始化全局变量
				isActiveNote: false,
				isActiveStore: false,
				mid: '',
				today: utils.getToDay(),
				dateTimekey: '',
				activeIndex: '',
				label01: '单号', // 主表的第一个label
				activeLineIndex: 0,
				INSTATIONNAME: '',
				PK_INSTATION: ''
			}
		},
		components: {
			wPicker
		},
		onLoad(p) {
			console.log(JSON.stringify(p));
			_self = this
			this.bstype = p.t
			query.userId = _self.userInfo.UserId
			query.account = _self.userInfo.Account
			query.category = p.dbt;
			/* console.log(p.item); */
			let abc = JSON.parse(decodeURIComponent(p.item));
			abc.PK_ORG = '';
			abc.ORGNAME = '';
			abc.STORENAME = '';
			abc.PK_STORE = '';
			_self.mainOrder = abc;
			_self.mainOrder.PK_GROUP = abc.PK_GROUP;
			/* console.log(abc.PK_GROUP);
			console.log(_self.mainOrder.PK_GROUP); */
			ajaxUrl = api.apiPublic.DisplacementAdd;

		},
		onShow() {
			uni.$on('getSelectItem', function(data) {
				console.log(data);
				switch (activeType) {
					case 'GROUP':
						_self.mainOrder.GROUPNAME = data.FullName
						_self.mainOrder.PK_GROUP = data.OrganizeId
						break;
					case 'BA_ORGANIZE':
						_self.mainOrder.ORGNAME = data.FullName
						_self.mainOrder.PK_ORG = data.OrganizeId
						break;
					case 'BD_RACK':
						console.log(data.VNAME)
						_self.dataList.INSTATIONNAME = data.VNAME;
						_self.dataList.PK_INSTATION = data.BILLNO;
					default:
						break;
				}
			})


		},
		onUnload() {
			uni.$off('getSelectItem', function(data) {})
		},
		methods: {
			getNote(e) {
				VMEMO = e.target.value;
			},
			onConfirm(val) {
				this.dataList[activeIndex][dateTimekey] = val.result;
			},
			dateEvent(id, index) {
				dateTimekey = id;
				activeIndex = index;
				this.$refs.dateTime.show();
			},
			activeNote() {
				_self.isActiveNote = true
			},
			activeIndexM(i, t) {
				_self.activeLineIndex = i;
				if (i == 5) {
					return;
				}
				activeType = t;
				uni.navigateTo({
					url: '../public/search?t=' + t + "&organizeId=" + _self.mainOrder.PK_GROUP
				})
			},
			openScan: function(e, index) {
				//扫码
				uni.scanCode({
					onlyFromCamera: true, //只能从相机
					success: function(res) {
						let codeValue = res.result;
						let codeUrl = _self.baseUrl + api.apiPublic.ScanCode;
						console.log(e);
						console.log(codeValue);
						if (e == 'STORENAME') {
							let t = res.result.replace(/'/g, '"');
							let data = JSON.parse('{' + t + '}');
							_self.mainOrder.PK_STORE = data.PK_STORE;
							_self.mainOrder.STORENAME = data.STORENAME;
							/* _self.mainOrder.PK_ORG = data.PK_ORG;
							_self.mainOrder.ORGNAME = data.ORGNAME; */
						} else if (e == 'PK_MATERIAL') {

							this.axios({
								url: codeUrl ,
								data: {Code: codeValue} ,
								isLoading: 1
							}).then(res=>{
								let { Result } = res ;
								let List = Result;
								if (_self.allData.length > 0) {
									let p = 0;
									_self.allData.forEach((item, index) => {
										if (item.BARCODE != List.BARCODE) {
											p += 0;
								
										} else {
											p += 1;
										}
									})
									if (p == 0) {
										_self.dataList.push(List);
									} else {
										uni.showToast({
											title: '不能重复扫该条码',
											icon: 'none',
											duration: 2000
										});
										return;
									}
								} else {
									_self.dataList.push(List);
								}
								_self.allData = _self.dataList;
							})
						} else if (e == 'INSTATIONNAME') {
							let t = res.result.replace(/'/g, '"');
							let data = JSON.parse('{' + t + '}');
							_self.INSTATIONNAME = data.STATIONNAME;
							_self.PK_INSTATION = data.PK_INSTATION;
							_self.dataList[index].INSTATIONNAME = data.STATIONNAME || ' iopi ';
							_self.dataList[index].PK_INSTATION = data.PK_STATION || ' oii ';
							/* 	console.log(_self.dataList[index]); */
						}

						console.log("获取二维码数据结束");
					}
				});
				console.log("相机加载结束");
			},
			//选择图片
			chooseImage: async function() {
				uni.chooseImage({

					sourceType: sourceType[this.sourceTypeIndex],
					// #ifdef MP-WEIXIN
					sizeType: sizeType[this.sizeTypeIndex],
					// #endif
					count: this.imageLength - this.imageList.length,
					success: (res) => {
						console.log(_self.baseUrl);
						var _tempFiles = res.tempFiles[0];
						console.log(res.tempFiles[0].path)
						var _tempFile = res.tempFiles[0].path;

						console.log(_tempFiles);
						this.imageList = this.imageList.concat(res.tempFilePaths);
						//this.imageList = this.imageList.concat(res.tempFiles[0]);
						console.log(this.imageList);
						const tempFilePaths2 = res.tempFilePaths;
						debugger;
						//上传图片接口
						var imgUrl = _self.baseUrl + "/UploadFile/UploadifyFile";

						console.log(imgUrl);
						uni.uploadFile({
							url: imgUrl,
							filePath: _tempFile,
							name: 'file',
							header: {
								'content-type': 'multipart/form-data'
							},

							success: function(uploadFileRes) {
								_self.condations.INSTALLPHOTO = uploadFileRes.data;
							}
						});

					}
				})
			},
			dateChangeEnd(e) {
				this.condations.endDate = e.target.value;
			},
			//预览图片
			previewImage: function(e) {
				var current = e.target.dataset.src
				uni.previewImage({
					current: current,
					urls: this.imageList
				})
			},
			//删除图片
			deleteImage: function(e) {
				var index = e.target.dataset.index;
				var that = this;
				var images = that.imageList;
				images.splice(index, 1);
				that.imageList = images;
			},

			submit() {
				let data = {
					"MOVETYPE": 0,
					"BTCATEGORY": 0,
					"GROUPNAME": _self.mainOrder.GROUPNAME,
					"PK_GROUP": _self.mainOrder.PK_GROUP,
					"PK_STORE": _self.mainOrder.PK_STORE,
					"STORENAME": _self.mainOrder.STORENAME,
					"PK_ORG": _self.mainOrder.PK_ORG,
					"ORGNAME": _self.mainOrder.ORGNAME,
					"MOVER": _self.mainOrder.MOVER,
					"PK_MOVER": _self.mainOrder.PK_MOVER,
					"BILLDATE": utils.getToDay(),
					"BILLMAKER": _self.mainOrder.BILLMAKER,
					"VMEMO": VMEMO,
					"userId": _self.userInfo.UserId,
					"account": _self.userInfo.Account,
					"Detail": JSON.stringify(_self.dataList)
				}
				/* console.log(data) */
				let checkResult = false,
					error = '';
				_self.dataList.forEach((item) => {
					if (!item.INSTATIONNAME) {
						return error = '移入货位不能为空'
					} else if (!item.NNUM) {
						return error = '移入数量不能为空'
					}
					checkResult = true;
				})

				if (!data.PK_STORE) {
					return uni.showToast({
						title: '仓库不能为空',
						icon: "none"
					})
				} else if (!checkResult) {
					return uni.showToast({
						title: error,
						icon: "none"
					})
				}
				/* 	console.log(ajaxUrl) */
				_self.isDisabled = true
				this.axios({
					url: ajaxUrl ,
					data: data ,
					isLoading: 1
				}).then(res=>{
					let { Result } = res ;
					this.$utils.tips('提交成功')
					utils.delayBack();
				})
			}
		}
	}
</script>

<style scoped>
	.form-container{
		padding: 0 40rpx;
	}
	.wrap-row{
		padding: 30rpx 0;
		border-bottom: 1rpx solid #F1F1F1;
		align-items: center;
	} 
	.scan-btn {
		top: -10rpx;
		right: 0;
		font-size: 30px;
		padding: 0 0 0 27rpx;
	}
	
	.down-arrow-btn {
		top: -20upx;
		right: 0upx;
		font-size: 40upx;
		padding: 10upx;
	}
</style>
